//
//  AUthenTopTypeButton.swift
//  kxf
//
//  Created by Mac on 2024/3/6.
//

import Foundation
import UIKit

class AUthenTopTypeButton: UIView {
    
    var isSelected = false
    var tapBlock:(()->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: frame.size.height))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
        self.addGestureRecognizer(tap)
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var titleLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 14, weight: .regular)
        
        return lab
    }()
    lazy var lineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .THEME
        vi.layer.cornerRadius = kFitWidth(2)
        vi.clipsToBounds = true
        vi.isHidden = true
        return vi
    }()
}

extension AUthenTopTypeButton{
    func setSelected(isSelect:Bool) {
        isSelected = isSelect
        if isSelected{
            titleLabel.textColor = .black
            titleLabel.font = .systemFont(ofSize: 14, weight: .medium)
            lineView.isHidden = false
        }else{
            titleLabel.textColor = .COLOR_TEXT_BLACK666
            titleLabel.font = .systemFont(ofSize: 14, weight: .regular)
            lineView.isHidden = true
        }
    }
    @objc func tapAction(){
        setSelected(isSelect: true)
        if self.tapBlock != nil{
            self.tapBlock!()
        }
    }
}

extension AUthenTopTypeButton{
    func initUI() {
        addSubview(titleLabel)
        addSubview(lineView)
        
        setConstrait()
    }
    func setConstrait() {
        titleLabel.snp.makeConstraints { make in
            make.center.lessThanOrEqualToSuperview()
        }
        lineView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.centerX.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(26))
            make.height.equalTo(kFitWidth(4))
        }
    }
}
